package com.mooer.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.mooer.R;
import com.mooer.bean.ShopActivity;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;
import com.mooer.utils.ui.BitmapViewBinder;
import com.mooer.utils.ui.ProgressLinearLayout;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-22 上午11:13:58 类说明
 */
public class ActivityOld extends Activity implements OnClickListener {

	private ListView listView;

	private ProgressLinearLayout footer;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	private SimpleAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_over);
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		adapter = new SimpleAdapter(this, list,
				R.layout.activity_list_over_item, new String[] { "name",
						"content" }, new int[] { R.id.activity_name,
						R.id.activity_content });
		adapter.setViewBinder(new BitmapViewBinder());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				Intent intent = new Intent(ActivityOld.this,
						ActivityDetail2.class);
				intent.putExtra("activityId",
						(Integer) list.get(position).get("activityId"));
				intent.putExtra("title", (String) list.get(position)
						.get("name"));
				intent.putExtra("content",
						(String) list.get(position).get("content"));
				startActivity(intent);
			}
		});
		footer.setOnClickListener(this);
		new ActivityOldTask().execute("");
	}

	public class ActivityOldTask extends
			AsyncTask<String, Integer, List<ShopActivity>> {

		private boolean isException;

		@Override
		protected List<ShopActivity> doInBackground(String... params) {
			List<ShopActivity> list = null;
			try {
				list = HttpUtils.fetchShopActivityList(-1, 0, 10);
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<ShopActivity> result) {
			super.onPostExecute(result);

			if (isException) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.VISIBLE);
			} else {
				if (result.size() > 0) {
					for (ShopActivity shopActivity : result) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", shopActivity.getSummary());
						map.put("content", shopActivity.getContent());
						map.put("activityId", shopActivity.getId());
						list.add(map);
					}
					listView.removeFooterView(footer);
					adapter.notifyDataSetChanged();
				} else {
					footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
							View.GONE);
					footer.setText(null, "往期暂无活动");
				}
			}

		}

	}

	@Override
	public void onClick(View v) {
		if (v == footer.reload) {

		}

	}

}
